/**
 * https://leetcode.cn/problems/partition-list-lcci/description/
 */

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* partition(ListNode* head, int x) {
        if(head==NULL)
        return head;
        ListNode* p=head;
        while(p!=NULL&&p->val<x)
        p=p->next;
        if(p!=NULL){
            ListNode* mp=p->next;
        while(mp!=NULL){
            if(mp->val<x){
                int k=mp->val;
                mp->val=p->val;
                p->val=k;
                p=p->next;
            }
            mp=mp->next;
        }
        }
        return head;
    }
};